﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class 原料附件 : System.Web.UI.Page
    {
        private void OnError(string msg)
        {
            this.Panel2.Visible = false;
            this.Panel_Msg.Visible = true;
            this.Message.Text = "·"+msg;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string opp3 = (string)Session["OPP3"];
#if DEBUG
            opp3 = ",1,2,3,4,5,6,";
#endif
            if (opp3 == null || opp3 == ",")
            {
                OnError("权限不足");
                return;
            }
            else
            {
                if (opp3.StartsWith(","))
                {
                    opp3 = opp3.Substring(1);
                }
                if (opp3.EndsWith(","))
                {
                    opp3 = opp3.Substring(0, opp3.Length - 1);
                }
            }
            int id = 0;
            int oid = 0;
            if (!this.IsPostBack)
            {
                //判定是否传递了ID(入库单ID)
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }
                if (Request.QueryString["OID"] != null)
                {
                    int.TryParse(Request.QueryString["OID"], out oid);
                }
                if (id > 0)//传递了ID
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = "select 到货记录 from 原料入库单 where [ID]=" + id;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.Label_List.Text = (string)Reader[0];
                        Bind();
                    }
                    else
                    {
                        OnError("原料入库单不存在");
                    }

                    Reader.Close();
                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
                else if (oid > 0)
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = "select distinct 管材到货登记ID from 管材到货明细 where 出库单ID=" + oid;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    string tmp = ",";
                    while (Reader.Read())
                    {
                        tmp = tmp + ((int)Reader[0]).ToString();
                        
                    }

                    Reader.Close();
                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();

                    if (tmp == ",")
                    {
                        OnError("记录不存在");
                    }
                    else
                    {
                        this.Button_DH.Text = "出库明细";
                        this.Label_Title.Text = "原料出库明细";
                        this.Label_List.Text = tmp.Substring(1);
                        Bind();
                    }
                }
                else//没有传递ID
                {
                    OnError("无效的访问方式");
                }
            }
        }
        private void Bind()
        {
            string[] lists = this.Label_List.Text.Split(",".ToCharArray());
            this.Label_CheShu.Text = lists.Length.ToString();
            int page = int.Parse(this.Label_Page.Text);
            if (lists.Length >= page)
            {
                this.SqlDataSource1.SelectCommand =
@"select 原料.品名,
原料.钢级,
原料.规格,
原料供货单位.名称 as 供货单位,
管材到货登记.车号,
管材到货登记.支数,
管材到货登记.重量,
管材到货登记.磅量,
管材到货登记.到货日期,
管材到货登记.质检员
from 管材到货登记
inner join 原料 on 管材到货登记.原料ID=原料.ID
inner join 原料供货单位 on 管材到货登记.原料供货单位ID=原料供货单位.ID
where 管材到货登记.ID=" + lists[page - 1];
                this.SqlDataSource2.SelectCommand = "select * from 管材到货明细 where 管材到货登记ID=" + lists[page - 1];
                this.DataList1.DataBind();
                this.DataList2.DataBind();
                this.Repeater1.DataBind();
            }
            if (page > 1)
            {
                this.Button_Prev.Visible = true;
            }
            else
            {
                this.Button_Prev.Visible = false;
            }
            if (page < lists.Length)
            {
                this.Button_Next.Visible = true;
            }
            else
            {
                this.Button_Next.Visible = false;
            }

        }

        protected void Button_Next_Click(object sender, EventArgs e)
        {
            string[] lists = this.Label_List.Text.Split(",".ToCharArray());
            int page = int.Parse(this.Label_Page.Text);
            if (page < lists.Length)
            {
                page++;
                this.Label_Page.Text = page.ToString();
            }
            Bind();
        }

        protected void Button_Prev_Click(object sender, EventArgs e)
        {
            int page = int.Parse(this.Label_Page.Text);
            if (page >1)
            {
                page--;
                this.Label_Page.Text = page.ToString();
            }
            Bind();
        }

        protected void Button_PD_Click(object sender, EventArgs e)
        {
            this.Button_PD.Visible = false;
            this.Button_DH.Visible = true;

            this.Panel3.Visible = false;
            this.Panel4.Visible = true;
        }

        protected void Button_DH_Click(object sender, EventArgs e)
        {
            this.Button_PD.Visible = true;
            this.Button_DH.Visible = false;

            this.Panel3.Visible = true;
            this.Panel4.Visible = false;
        }
    }
}
